package com.ruoyi.business.controller;

import com.ruoyi.common.core.domain.entity.CustomerClass;
import com.ruoyi.business.service.ICustomerClassService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.Customer;
import com.ruoyi.business.service.ICustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.Arrays;
import java.util.List;

/**
 * CustomerController
 *
 * @author liutietou
 * @since 1.0 2025-06-27
 */
@RestController
@RequestMapping("/business/Customer")
public class CustomerController extends BaseController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICustomerClassService customerClassService;

    /**
     * 查询Customer列表
     */
    @PreAuthorize("@ss.hasPermi('business:Customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(Customer customer) {
        Page<Customer> page = customerService.findCustomerPage(customer);
        return getDataTable(page);
    }

    @PreAuthorize("@ss.hasPermi('business:Customer:list')")
    @GetMapping("/selectlist")
    @ResponseBody
    public AjaxResult selectlist(Customer customer) {
        List<Customer> list = customerService.findCustomer(customer);
        return AjaxResult.success(list);

    }

    /**
     * 导出Customer列表
     */
    @PreAuthorize("@ss.hasPermi('business:Customer:export')")
    @Log(title = "Customer", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Customer customer) {
        List<Customer> list = customerService.findCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<>(Customer.class);
        return util.exportExcel(list, "Customer");
    }

    /**
     * 获取Customer详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:Customer:query')")
    @GetMapping(value = "/{ccuscode}")
    public AjaxResult getInfo(@PathVariable("ccuscode") String ccuscode) {
        return AjaxResult.success(customerService.findById(ccuscode));
    }

    /**
     * 新增Customer
     */
    @PreAuthorize("@ss.hasPermi('business:Customer:add')")
    @Log(title = "Customer", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer) {
        customerService.save(customer);
        return AjaxResult.success();
    }

    /**
     * 修改Customer
     */
    @PreAuthorize("@ss.hasPermi('business:Customer:edit')")
    @Log(title = "Customer", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer) {
        customerService.save(customer);
        return AjaxResult.success();
    }

    /**
     * 删除Customer
     */
    @PreAuthorize("@ss.hasPermi('business:Customer:remove')")
    @Log(title = "Customer", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ccuscodes}")
    public AjaxResult remove(@PathVariable String[] ccuscodes) {
        customerService.deleteByIds(Arrays.asList(ccuscodes));
        return AjaxResult.success();
    }


    @GetMapping("/treeselect")
    public AjaxResult treeselect(CustomerClass customerClass) {
        List<CustomerClass> list = customerClassService.selectCustomerClassList(customerClass);
        return AjaxResult.success(customerClassService.buildCustomerClassTree(list));
    }
}
